import { Root } from "../../../../Core/Module/Entity/Root";
import { AEvent } from "../../../../Core/Module/EventSystem/IEvent";
import { EventAttribute } from "../../../../Decorator/Decorator";
import { ETTask } from "../../../../Support/Share/Lib/Async/ETTask";
import { StartSceneConfigCategory } from "../../../Model/Generate/Server/Config/StartSceneConfig";
import { ActorMessageDispatcherComponent } from "../../../Model/Server/Module/Actor/ActorMessageDispatcherComponent";
import { ActorMessageSenderComponent } from "../../../Model/Server/Module/Actor/ActorMessageSenderComponent";
import { ActorLocationSenderComponent } from "../../../Model/Server/Module/ActorLocation/ActorLocationSenderComponent";
import { LocationProxyComponent } from "../../../Model/Server/Module/ActorLocation/LocationProxyComponent";
import { NetInnerComponent } from "../../../Model/Server/Module/Message/NetInnerComponent";
import { EntryEventType } from "../../../Model/Share/Entry";
import { ServerSceneManagerComponent } from "../../../Model/Share/Module/Scene/ServerSceneManagerComponent";
import { SceneFactory } from "./Helper/SceneFactory";
import { DBManagerComponent } from "../../../Model/Server/Module/DB/DBManagerComponent";
import { StartProcessConfigCategory } from "../../../Model/Generate/Server/Config/StartProcessConfig";
import { AppType, Options } from "../../../../Core/Module/Options/Options";

@EventAttribute(EntryEventType.EntryEvent2)
export class EntryEvent2_InitServer extends AEvent<EntryEventType.EntryEvent2> {

    async Run(a: EntryEventType.EntryEvent2) {
        // 发送普通actor消息
        Root.Instance.Scene.AddComponent<ActorMessageSenderComponent>(ActorMessageSenderComponent);
        // 发送location actor消息
        Root.Instance.Scene.AddComponent<ActorLocationSenderComponent>(ActorLocationSenderComponent);
        // 访问location server的组件
        Root.Instance.Scene.AddComponent<LocationProxyComponent>(LocationProxyComponent);
        Root.Instance.Scene.AddComponent<ActorMessageDispatcherComponent>(ActorMessageDispatcherComponent);
        Root.Instance.Scene.AddComponent<ServerSceneManagerComponent>(ServerSceneManagerComponent);

        Root.Instance.Scene.AddComponent<DBManagerComponent>(DBManagerComponent);

        const processConfig = StartProcessConfigCategory.Instance.Get(Options.Instance.Process);
        switch (Options.Instance.AppType) {
            case AppType.Server:
                Root.Instance.Scene.AddComponent<NetInnerComponent, string>(NetInnerComponent, processConfig.InnerIPPort);
                const processScenes = StartSceneConfigCategory.Instance.GetByProcess(Options.Instance.Process);
                for (const startConfig of processScenes) {
                    await SceneFactory.CreateServerScene(ServerSceneManagerComponent.Instance, startConfig.Id, startConfig.InstanceId, startConfig.Zone, startConfig.Name, startConfig.Type, startConfig);
                }
                break;
        }

        await ETTask.CompletedTask;
    }

}